package ee.android.kinokavad.data;

import java.io.Serializable;
import java.util.Locale;

import com.google.gson.annotations.SerializedName;

import ee.android.kinokavad.utils.LogUtils.Logger;

@SuppressWarnings("serial")
public class Movie implements Serializable{

	private long netId;
	
	private long id;

	private String title;

	private String originalTitle;
	
	private String description;

	private String genre;

	private String youtubeLink;

	private String imdbLink;
	
	private String pictureLink;
	
	@SerializedName("Poster")
	private String poster;
	@SerializedName("Runtime")
	private String duration;
	@SerializedName("imdbRating")
	private String imdbRating;
	@SerializedName("Response")
	private boolean omdbResponse;


	public Movie(long id,long netId, String title, String originalTitle,
			String description, String genre, String youtubeLink, String imdbLink, String pictureLink) {
		this.id = id;
		this.netId = netId;
		this.title = title;
		this.originalTitle = originalTitle;
		this.description = description;
		this.genre = genre;
		this.youtubeLink = youtubeLink;
		this.imdbLink = imdbLink;
		this.pictureLink = pictureLink;
	}
	
	/**
	 * Constructor for database
	 * @param id
	 * @param netId
	 * @param title
	 * @param description
	 * @param genre
	 * @param youtubeLink
	 * @param imdbLink
	 * @param pictureLink
	 * @param poster
	 * @param imdbRating
	 * @param duration
	 */

	public Movie(long id, long netId, String title, String originalTitle, String description,
			String genre, String youtubeLink, String imdbLink,
			String pictureLink, String poster, String imdbRating,
			String duration) {
		super();
		this.id = id;
		this.netId = netId;
		this.title = title;
		this.originalTitle = originalTitle;
		this.description = description;
		this.genre = genre;
		this.youtubeLink = youtubeLink;
		this.imdbLink = imdbLink;
		this.pictureLink = pictureLink;
		this.poster = poster;
		this.imdbRating = imdbRating;
		this.duration = duration;
	}



	public long getNetId() {
		return netId;
	}

	public String getTitle() {
		return title;
	}

	public String getDescription() {
		return description;
	}

	public String getGenre() {
		return genre;
	}

	public String getYoutubeLink() {
		return youtubeLink;
	}

	public String getImdbLink() {
		return imdbLink;
	}

	public String getPictureLink() {
		return pictureLink;
	}

	public void setPictureLink(String pictureLink) {
		this.pictureLink = pictureLink;
	}

	@Override
	public String toString() {
		return "Movie [netId=" + netId + ", id=" + id + ", title=" + title
				+ ", description=" + description + ", genre=" + genre
				+ ", youtubeLink=" + youtubeLink + ", imdbLink=" + imdbLink
				+ ", pictureLink=" + pictureLink + "]";
	}

	public String toLongString() {
		return "Movie [netId=" + netId + ", id=" + id + ", title=" + title
				+ ", description=" + description + ", genre=" + genre
				+ ", youtubeLink=" + youtubeLink + ", imdbLink=" + imdbLink
				+ ", pictureLink=" + pictureLink + ", poster=" + poster
				+ ", duration=" + duration + ", imdbRating=" + imdbRating + "]";
	}

	public String getPoster() {
		return poster;
	}

	public void setPoster(String poster) {
		this.poster = poster;
	}

	public String getDuration() {
		
		try {
			if (duration != null && !duration.contains("h") 
					&& !duration.toLowerCase(Locale.ENGLISH).contains("n/a")) {
				String copy = duration.replace("min", "").replace(" ", "");
				int minutes = Integer.parseInt(copy);
				if (minutes < 60) return duration;
				
				int hours = minutes/60;
				return String.format(Locale.ENGLISH, "%d h %d min", hours, minutes % 60);
			}
		} catch (Exception e) {
			Logger.e("Movie", "Failed to parse duration: " + duration);
		}
		
		return duration;
	}

	public void setDuration(String duration) {
		this.duration = duration;
	}

	public String getImdbRating() {
		return imdbRating;
	}

	public void setImdbRating(String imdbRating) {
		this.imdbRating = imdbRating;
	}

	public boolean hasOmdbResponse() {
		return omdbResponse;
	}
	public void setNetId(long netId) {
		this.netId = netId;
	}

	public String getOriginalTitle() {
		return originalTitle;
	}

	public void setOriginalTitle(String originalTitle) {
		this.originalTitle = originalTitle;
	}

}
